package x.zero.service.sys;


import cn.hutool.core.util.NumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import x.zero.mapper.SysPermissionMapper;
import x.zero.mapper.SysRoleMapper;
import x.zero.mapper.SysRolePermissionMapper;
import x.zero.pojo.entity.SysRole;
import x.zero.pojo.entity.SysRolePermission;
import x.zero.util.UserUtils;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 */
@Service
public class SysRoleService extends BaseService<SysRole> {

    @Autowired
    private SysPermissionMapper permissionMapper;
    @Autowired
    private SysRolePermissionMapper rolePermissionMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysRolePermissionMapper rolePermissionRelationMapper;
    @Autowired
    private SysRolePermissionService rolePermissionRelationDao;
    @Autowired
    private SysUserService userService;

    /*public List<SysPermission> getPermissionList(Long roleId) {
        SysRolePermission srp = new SysRolePermission();
        srp.setRoleId(roleId);
        return rolePermissionMapper.template(srp);
    }*/

    public List<SysRolePermission> getRolePermission(Long roleId) {
        SysRolePermission srp = new SysRolePermission();
        srp.setRoleId(roleId);
        return rolePermissionMapper.template(srp);
    }

    @Transactional
    public boolean saves(SysRole role) {
        role.setId(null);
        role.setCreateTime(new Date());
        role.setStatus(1);
        role.setAdminCount(0);
        role.setSort(0);
        roleMapper.insertTemplate(role);
        updatePermission(role.getId(), role.getMenuIds());
        return true;
    }

    /**
     * 删除角色与权限
     */
    @Transactional
    public void deleteRole(Long id){
        roleMapper.deleteById(id);
        rolePermissionRelationMapper.deleteByRoleId(id);
    }

    @Transactional
    public boolean updates(SysRole role) {
        role.setId(role.getId());
        updatePermission(role.getId(), role.getMenuIds());
        roleMapper.updateById(role);
        return true;
    }
    @Transactional
    public void updatePermission(Long roleId, String permissionIds) {

        //先删除原有关系
        rolePermissionRelationMapper.deleteByRoleId(roleId);
        //批量插入新关系
        List<SysRolePermission> relationList = new ArrayList<>();
        if (!StringUtils.isEmpty(permissionIds)) {
            String[] mids = permissionIds.split(",");
            for (String permissionId : mids) {
                SysRolePermission relation = new SysRolePermission();
                relation.setRoleId(roleId);
                if(NumberUtil.isNumber(permissionId)){
                    relation.setPermissionId(Long.valueOf(permissionId));
                    relationList.add(relation);
                }

            }
            rolePermissionRelationMapper.insertBatch(relationList);
        }
        userService.removePermissRedis(UserUtils.getCurrentMember().getId());
    }
}
